<?php

namespace App\Remote\Mybank;

use App\Exceptions\BaseException;
use App\Kuafu\Mybank\Enum\MybankFunctionEnum;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class MybankAccountRemote extends Remote
{

    /**
     * 商户余额查询
     * @param string $merchantId
     * @return mixed
     * @throws GuzzleException|BaseException
     */
    public static function merchantBalanceQuery(string $merchantId)
    {
        $params = [
            'IsvOrgId'   => Remote::getIsvOrgId(),
            'MerchantId' => $merchantId,
        ];

        return self::customRequestV2(
            config('pay.mybank.sbServerUrl'),
            RequestAlias::METHOD_POST,
            self::buildXmlRequestData($params, MybankFunctionEnum::BKCLOUDFUNDS_MERCHANT_SCENE_BALANCE_QUERY)
        );
    }


    /**
     * 子户余额查询
     * @param string $merchantId
     * @return mixed
     * @throws GuzzleException|BaseException
     */
    public static function accountBalanceQuery()
    {
        $params = [
            'IsvOrgId'    => Remote::getIsvOrgId(),
            'BankCardNo'  => Remote::getBankCard(),
            'AccountType' => '01',
        ];

        return self::customRequestV2(
            config('pay.mybank.sbServerUrl'),
            RequestAlias::METHOD_POST,
            self::buildXmlRequestData($params, MybankFunctionEnum::BKCLOUDFUNDS_ACCOUNT_BALANCE_QUERY)
        );
    }


    /**
     * 明细列表
     * @param string $merchantId
     * @return mixed
     * @throws GuzzleException|BaseException
     */
    public static function accountVostroBatchQuery()
    {
        $params = [
            'IsvOrgId'     => Remote::getIsvOrgId(),
            'VostroCardNo' => Remote::getBankCard(),
            'StartTime'    => '20230523',
            'EndTime'      => '20230530',
            'PageSize'     => 50
        ];

        return self::customRequestV2(
            config('pay.mybank.bkServerUrl'),
            RequestAlias::METHOD_POST,
            self::buildXmlRequestData($params, MybankFunctionEnum::BKCLOUDFUNDS_VOSTRO_BATCHQUERY)
        );
    }


    /**
     * 余额解冻
     * @param string $merchantId
     * @return mixed
     * @throws GuzzleException|BaseException
     */
    public static function unfreezeApply(string $merchantId, int $amount, string $outTradeNo)
    {
        $params = [
            'IsvOrgId'    => Remote::getIsvOrgId(),
            'MerchantId'  => $merchantId,
            'OutTradeNo'  => $outTradeNo,
            'AccountType' => 'BALANCE',
            'Amount'      => $amount,
            'Currency'    => 'CNY'
        ];

        return self::returnBodyData(self::customRequestV2(
            config('pay.mybank.bkServerUrl'),
            Request::METHOD_POST,
            self::buildXmlRequestData($params, MybankFunctionEnum::BKCLOUDFUNDS_MERCHANT_ACCOUNT_UNFREEZE_APPLY)
        ));
    }

}
